1 using System;
2 using
System.Collections.Generic;
3 using
System.ComponentModel;
4 using
System.Data;
5 using
System.Drawing;
6 using
System.Linq;
7 using
System.Text;
8 using
System.Windows.Forms;
9 using
System.Data.SqlClient;
10 namespace
WarehouseManagementSystem
11 {
12     
public partial class frmCategory : Form
13     {
14         SqlDataReader rdr =
null;
15         DataTable dtable =
new DataTable();
16         SqlConnection con =
null;
17         SqlCommand cmd =
null;
18         DataTable dt =
new DataTable();
19         ConnectionString cs =
new ConnectionString();
20
21
22         
public frmCategory()
23         {
24             InitializeComponent();
25         }
26
27         
private void frmCategory_Load(object sender, EventArgs e)
28         {
29             Autocomplete();
30         }
31
32         
private void btnSave_Click(object sender, EventArgs e)
33         {
34             
if (txtCategoryName.Text == "")
35             {
36                 MessageBox.Show(
"Please enter Category name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
37                 txtCategoryName.Focus();
38                 
return;
39             }
40
41
42             
try
43             {
44                 
45                 con =
new SqlConnection(cs.DBConn);
46                 con.Open();
47                 
string ct = "select CategoryName from Category where CategoryName='" + txtCategoryName.Text + "'";
48
49                 cmd =
new SqlCommand(ct);
50                 cmd.Connection = con;
51                 rdr = cmd.ExecuteReader();
52
53                 
if (rdr.Read())
54                 {
55                     MessageBox.Show(
"Category Name Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
56                     txtCategoryName.Text =
"";
57                     txtCategoryName.Focus();
58
59
60                     
if ((rdr != null))
61                     {
62                         rdr.Close();
63                     }
64                     
return;
65                 }
66
67                 con =
new SqlConnection(cs.DBConn);
68                 con.Open();
69
70                 
string cb = "insert into Category(CategoryName) VALUES ('" + txtCategoryName.Text + "')";
71
72                 cmd =
new SqlCommand(cb);
73                 cmd.Connection = con;
74                 cmd.ExecuteReader();
75                 con.Close();
76                 MessageBox.Show(
"Successfully saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
77                 Autocomplete();
78                 btnSave.Enabled =
false;
79
80             }
81             
catch (Exception ex)
82             {
83                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
84             }
85         }
86
87         
private void btnDelete_Click(object sender, EventArgs e)
88         {
89             
if (MessageBox.Show("Do you really want to delete this record?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
90             {
91                 delete_records();
92             }
93         }
94         
private void delete_records()
95         {
96
97             
try
98             {
99
100                 
int RowsAffected = 0;
101                 con =
new SqlConnection(cs.DBConn);
102                 con.Open();
103                 
string cq = "delete from Category where ID=" + txtID.Text + "";
104                 cmd =
new SqlCommand(cq);
105                 cmd.Connection = con;
106                 RowsAffected = cmd.ExecuteNonQuery();
107                 
if (RowsAffected > 0)
108                 {
109                     MessageBox.Show(
"Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
110                     Reset();
111                     Autocomplete();
112                 }
113                 
else
114                 {
115                     MessageBox.Show(
"No Record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information);
116                     Reset();
117                     Autocomplete();
118                 }
119                 
if (con.State == ConnectionState.Open)
120                 {
121                     con.Close();
122                 }
123
124
125             }
126             
catch (Exception ex)
127             {
128                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
129             }
130         }
131         
private void Autocomplete()
132         {
133             
try
134             {
135                 con =
new SqlConnection(cs.DBConn);
136                 con.Open();
137                 SqlCommand cmd =
new SqlCommand("SELECT distinct Categoryname FROM Category", con);
138                 DataSet ds =
new DataSet();
139                 SqlDataAdapter da =
new SqlDataAdapter(cmd);
140                 da.Fill(ds,
"Category");
141                 AutoCompleteStringCollection col =
new AutoCompleteStringCollection();
142                 
int i = 0;
143                 
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
144                 {
145                     col.Add(ds.Tables[
0].Rows[i]["Categoryname"].ToString());
146
147                 }
148                 txtCategoryName.AutoCompleteSource = AutoCompleteSource.CustomSource;
149                 txtCategoryName.AutoCompleteCustomSource = col;
150                 txtCategoryName.AutoCompleteMode = AutoCompleteMode.Suggest;
151
152                 con.Close();
153             }
154             
catch (Exception ex)
155             {
156                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
157             }
158         }
159         
private void btnUpdate_Click(object sender, EventArgs e)
160         {
161             
try
162             {
163                 
if (txtCategoryName.Text == "")
164                 {
165                     MessageBox.Show(
"Please enter Category name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
166                     txtCategoryName.Focus();
167                     
return;
168                 }
169                 con =
new SqlConnection(cs.DBConn);
170                 con.Open();
171
172                 
string cb = "update Category set CategoryName='" + txtCategoryName.Text + "' where ID=" + txtID.Text + "";
173                 cmd =
new SqlCommand(cb);
174                 cmd.Connection = con;
175                 cmd.ExecuteReader();
176                 con.Close();
177                 MessageBox.Show(
"Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
178                 Autocomplete();
179                 btnUpdate.Enabled =
false;
180             }
181             
catch (Exception ex)
182             {
183                 MessageBox.Show(ex.Message,
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
184             }
185         }
186         
private void Reset()
187         {
188             txtCategoryName.Text =
"";
189             btnSave.Enabled =
true;
190             btnDelete.Enabled =
false;
191             btnUpdate.Enabled =
false;
192             txtCategoryName.Focus();
193         }
194
195         
private void btnNew_Click(object sender, EventArgs e)
196         {
197             Reset();
198         }
199
200         
private void btnGetData_Click(object sender, EventArgs e)
201         {
202             
this.Hide();
203             frmCategoryRecord frm =
new frmCategoryRecord();
204             frm.Show();
205             frm.GetData();
206         }
207     }
208 }


Gõ tìm kiếm nhanh...